/*
 * Inspiration from http://www.antonioshome.net/kitchen/swingnbrcp/swingnbrcp-ui2.php and 
 * http://kenai.com/projects/swingnbrcp/sources/source/content/NBTWEETING/src/net/antonioshome/nbtweeting/gui/NBTweetingMainFrame.java?rev=2
 * Fixed hiding column 0 as per comment in http://layerxml.wordpress.com/2010/10/01/the-basic-of-nodes/
 */
package org.tintuna.cca.spreadsheetview;

import java.awt.BorderLayout;
import java.util.logging.Logger;
import javax.swing.ListSelectionModel;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
//import org.netbeans.swing.outline.Outline;
import org.netbeans.swing.outline.Outline;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.Node;
import org.tintuna.cca.data.Spreadsheet;
import org.tintuna.cca.node.RowNode;
import org.tintuna.cca.node.SpreadsheetNode;
//import org.netbeans.swing.outline.Outline;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.tintuna.cca.spreadsheetview//SpreadsheetView//EN",
autostore = false)
public final class SpreadsheetViewTopComponent extends TopComponent implements ExplorerManager.Provider {

    private static SpreadsheetViewTopComponent instance;
    private ExplorerManager mgr = new ExplorerManager();
    private Node spreadsheetnode;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "SpreadsheetViewTopComponent";

    public SpreadsheetViewTopComponent() {
//        initComponents();
//        initComponentsMe();
//        setName(NbBundle.getMessage(SpreadsheetViewTopComponent.class, "CTL_SpreadsheetViewTopComponent"));
//        setToolTipText(NbBundle.getMessage(SpreadsheetViewTopComponent.class, "HINT_SpreadsheetViewTopComponent"));
////        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
//        mgr.setRootContext((Node) new SpreadsheetNode(new Spreadsheet()));
        return;
        //this(new SpreadsheetNode(new Spreadsheet()));

    }

    public SpreadsheetViewTopComponent(Node spreadsheetnode) {
        initComponents();
        initComponentsMe();
        setName(NbBundle.getMessage(SpreadsheetViewTopComponent.class, "CTL_SpreadsheetViewTopComponent"));
        setToolTipText(NbBundle.getMessage(SpreadsheetViewTopComponent.class, "HINT_SpreadsheetViewTopComponent"));
        setSpreadsheetnode(spreadsheetnode);
        mgr.setRootContext((Node) getSpreadsheetnode());
        associateLookup(ExplorerUtils.createLookup(mgr, getActionMap()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();

        setLayout(new java.awt.BorderLayout());
        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void initComponentsMe() {
        outlineView = new OutlineView();
        //outlineView.getOutline().setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
        outlineView.setPropertyColumns(
                RowNode.DATE_PROPERTY, "Date",
                RowNode.AMOUNT_PROPERTY, "Amount",
                RowNode.TRANS_PROPERTY, "Transaction",
                RowNode.MERCHANT_PROPERTY, "Merchant",
                RowNode.BALANCE_PROPERTY, "Balance",
                RowNode.CATEGORIES_PROPERTY, "Categories");
        Outline outline = outlineView.getOutline();
        outline.setRootVisible(false);
        outline.getColumnModel().removeColumn(outline.getColumnModel().getColumn(0));
        add(outlineView, BorderLayout.CENTER);

    }
    private OutlineView outlineView = null;

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized SpreadsheetViewTopComponent getDefault() {
        if (instance == null) {
            instance = new SpreadsheetViewTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the SpreadsheetViewTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized SpreadsheetViewTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(SpreadsheetViewTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof SpreadsheetViewTopComponent) {
            return (SpreadsheetViewTopComponent) win;
        }
        Logger.getLogger(SpreadsheetViewTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return mgr;
    }

    public Node getSpreadsheetnode() {
        return spreadsheetnode;
    }

    public void setSpreadsheetnode(Node spreadsheetnode) {
        this.spreadsheetnode = spreadsheetnode;
    }
}
